package com.nseeod.db;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.nseeod.constant.AppConstants;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import java.util.Map;
import java.util.List;

public class DButil extends QueryRunner{
	public void load(String tableName ,String fileName) throws SQLException {
		Connection conn = null;
		String query = "LOAD DATA INFILE ? INTO TABLE "+ tableName +" FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES";
		PreparedStatement pstmt = null;
		System.out.println("Table :" + query);
		try {
			DbUtils.loadDriver(AppConstants.getJdbcDriver());
			conn = DriverManager.getConnection(AppConstants.getJdbcURL(), AppConstants.getUser(), AppConstants.getPassword());
			pstmt = prepareStatement(conn, query);
			pstmt.setString(1, fileName);
			//pstmt.(2, tableName);
			pstmt.execute();
			System.out.println("File Imported :" + fileName);
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
			
		} finally {
			try {
				DbUtils.close(pstmt);
			} catch (SQLException e) {
				throw e;
			}
			DbUtils.closeQuietly(conn);
		}
	}
}